package com.cmc.cloud.cmclink.doc.util.so;

import org.apache.commons.lang3.StringUtils;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * CodesUtil
 * @author chenzheng
 * @date 2025/09/15 11:25
 */
public class CodesUtil {
    private static final String[] GENERATE_SOURCE = new String[]{"1", "2", "3", "4", "5", "6", "7",
            "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
            "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z"};

    private static SecureRandom SECURE_RANDOM = new SecureRandom();

    /**
     * 生成随机字符串，generateByRandom的简化版
     *
     * @param retuenSize 随机字符串数量
     * @param keyLength  随机字符串的长度
     * @return List
     */
    public static List<String> doGenerate(int retuenSize, int keyLength) {
        List<String> codeList = new ArrayList<>();
        for (int k = 0; k < retuenSize; k++) {
            StringBuilder temp = new StringBuilder(keyLength);
            for (int i = 0; i < keyLength; i++) {
                final int index = SECURE_RANDOM.nextInt(GENERATE_SOURCE.length);
                temp.append(GENERATE_SOURCE[index]);
            }
            codeList.add(temp.toString());
        }
        return codeList.stream().distinct().collect(Collectors.toList());
    }

    public static void main(String[] args) {
        List<String> codeList = doGenerate(30, 3);
        Map<String, Long> codeCountMap = codeList.stream().collect(
                Collectors.groupingBy(e -> e, Collectors.counting())
        );
        for (Map.Entry<String, Long> e : codeCountMap.entrySet()) {
            if (e.getValue() > 1) {
                System.out.println(e.getKey() + " " + e.getValue());
            }
        }
        System.out.println(StringUtils.join(codeList, ","));

    }
}
